//package com.ucode.springboot.starter.web.config;
//
//import java.util.concurrent.Executor;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
//import org.apache.catalina.connector.Connector;
//import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
//import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
//import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.event.ContextClosedEvent;
//import cn.hutool.log.Log;
//import cn.hutool.log.LogFactory;
//
///**
// * 优雅安全停服
// * @author: liliang
// * @date: 2020年4月15日 下午3:45:27
// */
//public class SafeShutdownConfig {
//    
//    public static final int waitTime = 150;
//    
//    @Bean
//    public GracefulShutdownTomcat gracefulShutdownTomcat() {
//        return new GracefulShutdownTomcat();
//    }
//
//    @Bean
//    public ServletWebServerFactory servletContainer() {
//        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
//        tomcat.addConnectorCustomizers(gracefulShutdownTomcat());
//        return tomcat;
//    }
//    
//    /**
//     * tomcat 优雅停止
//     * 思路>>>获取Tomcat的Connector连接器，然后通过Connector获取其连接线程池，最终通过操作线程池安全终止来达到web请求也安全终止的目的
//     * @author: liliang
//     * @date: 2020年4月15日 下午3:51:34
//     */
//    private static class GracefulShutdownTomcat  implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {
//
//        private static Log log = LogFactory.get(GracefulShutdownTomcat.class);
//        /**
//         * Tomcat的Connector连接器
//         */
//        private volatile Connector connector;
//
//        @Override
//        public void customize(Connector connector) {
//            this.connector = connector;
//        }
//
//        @Override
//        public void onApplicationEvent(ContextClosedEvent event) {
//            log.info("application is going to stop. try to stop tomcat gracefully");
//            this.connector.pause();
//            Executor executor = this.connector.getProtocolHandler().getExecutor();
//            if (executor instanceof ThreadPoolExecutor) {
//                ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
//                threadPoolExecutor.shutdown();
//                try {
//                    if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
//                        log.info("Tomcat did not terminate in the specified time.");
//                        log.warn("Tomcat 进程在" + waitTime + " 秒内无法结束，尝试强制结束");
//                        threadPoolExecutor.shutdownNow();
//                    }
//                } catch (Exception ex) {
//                    log.error("awaitTermination failed.", ex);
//                    threadPoolExecutor.shutdownNow();
//                }
//            }
//        }
//    }
//
//}
